package com.jy.api.service.impl;

import com.jy.api.dao.CouponCensusDao;
import com.jy.api.dao.CouponDao;
import com.jy.api.dao.CouponInstanceDao;
import com.jy.api.domain.Coupon;
import com.jy.api.domain.CouponCensus;
import com.jy.api.domain.CouponInstance;
import com.jy.api.service.CouponCensusService;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by xiaohua on 2019/4/11.
 */
@Service
public class CouponCensusServiceImpl implements CouponCensusService {

@Autowired
private CouponInstanceDao couponInstanceDao;
@Autowired
private CouponCensusDao couponCensusDao;
@Autowired
private CouponDao couponDao;





    @Override
    public void clickAndShareCount(String couponId, String type) {
        Date date = new Date();

        CouponInstance couponInstance = couponInstanceDao.findOne(couponId);
        Coupon coupon = couponDao.findOne(couponInstance.getCouponId());
        CouponCensus couponCensus = couponCensusDao.findByCouponIdAndBeginTimeLessThanEqualAndEndTimeGreaterThanEqual(coupon.getId(),date, date);
        if (null == couponCensus) {
            CouponCensus census = new CouponCensus(coupon.getId(), MyDateUtils.getEarliest(new Date()), MyDateUtils.getLastest(new Date()));
            couponCensusDao.save(census);
        } else {
            if (StringUtils.equals("SHARE", type)) {
                couponCensus.setShareCount(couponCensus.getShareCount() + 1);
            } else {
                couponCensus.setClickCount(couponCensus.getClickCount() + 1);
            }
            couponCensusDao.save(couponCensus);
        }

    }



}
